package com.ifast.wechat.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alipay.api.AlipayRequest;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ifast.common.utils.Result;
import com.ifast.wechat.config.AlipayConfig;
import com.ifast.wechat.model.AlipaybizModel;
import com.ifast.wechat.model.entity.UserEntity;

/**
 * 
 * @author houzw
 *
 */
@RestController
@RequestMapping("/alipay/")
public class AlipayController {
	
	@Autowired
	private AlipayConfig alipayConfig;
	@GetMapping("/test")
	public Result<?> requireRole() {
		return Result.build(200, "请求成功！", null);
	}

	// 支付接口
	@ResponseBody
	@RequestMapping(value = "/pay" ,method = {RequestMethod.POST,RequestMethod.GET}, produces = "application/json;charset=UTF-8")
	public Result<?> pay (@RequestBody AlipaybizModel alipaybizModel) {
		Result<?> result = Result.build(-1, "支付失败！");
		try {
			// 构造client
			CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
			certAlipayRequest.setServerUrl(alipayConfig.getGateway());
			certAlipayRequest.setAppId(alipayConfig.getApp_id());
			certAlipayRequest.setPrivateKey(alipayConfig.getPrivateKey());
			certAlipayRequest.setFormat("json");
			certAlipayRequest.setCharset("utf-8");
			certAlipayRequest.setSignType("RSA2");
			certAlipayRequest.setCertPath(alipayConfig.getApp_cert_path());
			certAlipayRequest.setAlipayPublicCertPath(alipayConfig.getAlipay_cert_path());
			certAlipayRequest.setRootCertPath(alipayConfig.getAlipay_root_cert_path());
			DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
			// 发送API请求
			AlipayRequest<?> request = new AlipayTradeQueryRequest();
			// model
			AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
			
			model.setBody("我是测试数据");
			model.setSubject("App支付测试Java");
			model.setOutTradeNo("2018062800001");
			model.setTimeoutExpress("30m");
			model.setTotalAmount("0.01");
			model.setProductCode("STD_RED_PACKET");
			model.setTimeExpire("2019-10-23 19:15:00");
			model.setTimeoutExpress("2019-10-23 19:18:00");
			model.setBusinessParams("{\\\"sub_biz_scene\\\":\\\"REDPACKET\\\",\\\"payer_binded_alipay_uid:\\\"2088302510459335\\\"}");
			//request.setNotifyUrl("商户外网可以访问的异步地址");
	        // model
			request.setBizModel(model);
			AlipayTradeQueryResponse response = (AlipayTradeQueryResponse) alipayClient.certificateExecute(request);
			if(response.isSuccess()) {
				 result = Result.ok();
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = Result.build(-1, "支付失败！");
		}
		return result;
	}

	// 收款
	@SuppressWarnings("unused")
	@GetMapping("/rec")
	public String rec() {
		String result1 = "";
		try {

		} catch (Exception e) {
			e.printStackTrace();
		}
		return "收款成功";
	}

}
